<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Controller;

use Plugin\Attractions\AttractionManagement\Aspect\CheckJwtToken;
use Common\Common;
use Common\CommonController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Swagger\Annotation\HyperfServer;
use Plugin\Attractions\AttractionManagement\Service\UserService;

#[HyperfServer(name: 'http')]
#[Controller('attractions/attraction-management')]
class UserController extends CommonController
{
    #[Inject]
    private UserService $service;

    #[PostMapping('login')]
    public function login()
    {
        $data = $this->valid([
            'code' => 'string|required'
        ], []);
        $token = $this->service->login($data);
        return empty($token) ? $this->error() : $this->success($token);
    }

    #[PostMapping('deleteUser')]
    public function delete()
    {
        return $this->service->delete($this->valid([
            'id' => 'required|array',
        ], [])['id']) ? $this->success() : $this->error();
    }

    #[GetMapping('userInfo'), CheckJwtToken]
    public function userInfo()
    {
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        return $this->success($this->service->userInfo(['id' => $userId]));
    }

    #[PostMapping('update')]
    public function update() {}

    #[GetMapping('listUsers')]
    public function listUsers()
    {
        $data = $this->valid([
            'page' => 'integer',
            'pageSize' => 'integer',
            'name' => 'string',
            'phone' => 'string',
            'idCard' => 'string',
            'status' => 'integer'
        ], []);
        return $this->success($this->service->listUsers($data));
    }

    #[PostMapping('addTourist'), CheckJwtToken]
    public function addTourist()
    {
        $data = $this->valid([
            'name' => 'string|max:16|required',
            'phone' => 'string|max:16|required',
            'idCard' => 'string|max:32|required'
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $data['userId'] = $userId;
        return empty($this->service->createTourist($data)) ? $this->error() : $this->success();
    }

    #[GetMapping('deleteTourist'), CheckJwtToken]
    public function deleteTourist()
    {
        $data = $this->valid([
            'id' => 'required|regex:/^\d+(,\d+)*$/'
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $ids = explode(',', $data['id']);
        return $this->success($this->service->deleteTourist($ids, $userId));
    }

    #[PostMapping('updateTourist'), CheckJwtToken]
    public function updateTourist()
    {
        $data = $this->valid([
            'id' => 'required|regex:/^\d+(,\d+)*$/',
            'name' => 'string|max:16',
            'phone' => 'string|regex:/^1[3456789]\d{8}$/',
            'idCard' => 'string|regex:/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/'
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $ids = explode(',', $data['id']);
        unset($data['id']);
        return $this->success($this->service->updateTourist($ids, $data, $userId));
    }

    #[GetMapping('listTourist'), CheckJwtToken]
    public function listTourist()
    {
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        return $this->success($this->service->listTourist(['userId' => $userId]));
    }
}
